package com.wtwd.wtwddevice.listener.deviceServerMsg.handler.impl;


import com.wtwd.device.annotation.MsgType;
import com.wtwd.device.constant.AnswerStateEnum;
import com.wtwd.device.constant.CmdTypeEnum;
import com.wtwd.device.constant.MsgTypeEnum;
import com.wtwd.device.constant.SeparatorConstant;
import com.wtwd.device.model.DeviceStep;
import com.wtwd.device.mqtentity.MessageBase;
import com.wtwd.device.protocol.AppProtocol;
import com.wtwd.device.protocol.WTProtocol;
import com.wtwd.device.util.DateUtils;
import com.wtwd.wtwddevice.listener.deviceServerMsg.handler.IMsgHandler;
import com.wtwd.wtwddevice.model.entity.DeviceStepInfo;
import com.wtwd.wtwddevice.mq.PushMsgService;
import com.wtwd.wtwddevice.service.IAppCommService;
import com.wtwd.wtwddevice.service.IDeviceService;
import com.wtwd.wtwddevice.service.IDeviceStatusService;
import com.wtwd.wtwddevice.service.IDeviceStepService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;


@MsgType(MsgTypeEnum.REPORT_HEARTBEAT)
@Component
public class HeartBeatHandler implements IMsgHandler {


    @Resource
    private IDeviceStatusService deviceStatusService;

    @Resource
    private IDeviceService deviceService;

    @Resource
    private PushMsgService msgService;
    @Resource
    private IAppCommService appCommService;

    @Resource
    private IDeviceStepService stepService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleMsg(MessageBase messageBase) {
        WTProtocol wtProtocol = messageBase.getWtProtocol();
        String data = wtProtocol.getBody();
        String[] split = data.split(SeparatorConstant.CMD_BODY_REPORT_HEARTBEAT);
        AnswerStateEnum stateEnum = AnswerStateEnum.ABNORMAL;
        if (split.length == 3) {//心跳报文体区间正常开始解析
            stateEnum = AnswerStateEnum.NORMAL;
            String battery = split[0];//电量
            String step = split[1];//步数
            String wearingState = split[2];//佩戴状态
            Long deviceId = deviceService.getDeviceIdByImei(wtProtocol.getImei());
            deviceStatusService.updateStatus(deviceId, battery, step, wearingState);
            if (!step.equals("0")){
                DeviceStepInfo stepInfo = stepService.synchronization(deviceId, Integer.valueOf(step), wtProtocol.getTime(), messageBase.getType(), wtProtocol.getNumber());
                //推送步数数据给app
                DeviceStep deviceStep = new DeviceStep();
                BeanUtils.copyProperties(stepInfo, deviceStep);
                AppProtocol appProtocol = AppProtocol.builder()
                        .imei(wtProtocol.getImei())
                        .cmdName(MsgTypeEnum.PUSH_APP_STEP.getCmdName())
                        .data(deviceStep)
                        .build();
                appCommService.pushDeviceState(deviceId, appProtocol);
            }
        }
        wtProtocol.setTime(DateUtils.format(new Date(), DateUtils.dateFormat15));
        wtProtocol.setBody(String.valueOf(stateEnum.getState()));
        wtProtocol.setBodyLength(wtProtocol.getBody().length());
        wtProtocol.setMesType(CmdTypeEnum.REPORT_RESPONSE.getCmdType().toString());
        msgService.pushToDevice(messageBase.getForm(),
                MessageBase.builder().deviceKey(wtProtocol.getImei())
                        .wtProtocol(wtProtocol)
                        .build());
    }

}
